package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Author;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.BooksInformation;
import com.example.demo.entity.requestJson.DeleteReq;
import com.example.demo.entity.responseJson.HomePageBMoreInfoRes;
import com.example.demo.entity.responseJson.PerSpaceAInfoRes;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.AuthorMapper;
import com.example.demo.mapper.BooksInformationMapper;
import com.example.demo.service.APersonalSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/3/9 17:00
 * @description:
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class APersonalSpaceServiceImpl implements APersonalSpaceService {
    @Autowired
    private AuthorMapper authorMapper;
    @Autowired
    private BooksInformationMapper booksInformationMapper;
    /**
     * 个人中心作者基本信息
     */
    @Override
    public BackResponseEntity<PerSpaceAInfoRes> queryHomepageAuthorInfo(DeleteReq deleteReq){
        String id = null;

        if (!ObjectUtils.isEmpty(deleteReq.getId())) {
            id = deleteReq.getId()
                    .trim();
        }
        if (ObjectUtils.isEmpty(id)) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        LambdaQueryWrapper<Author> wrapper = new QueryWrapper<Author>().lambda();
        wrapper.select(Author::getAuthorNickname,Author::getAuthorId,Author::getAuthorImg,Author::getAuthorInfo,
                Author::getReaderCount,Author::getTotalWords,Author::getAttentionCount)
                .eq(!ObjectUtils.isEmpty(id), Author::getAuthorId, id);

        Author author = authorMapper.selectOne(wrapper);
        PerSpaceAInfoRes perSpaceAInfoRes = new PerSpaceAInfoRes();
        perSpaceAInfoRes.setAuthorId(author.getAuthorId());
        perSpaceAInfoRes.setAuthorInfo(author.getAuthorInfo());
        perSpaceAInfoRes.setAuthorNickname(author.getAuthorNickname());
        perSpaceAInfoRes.setAuthorImg(author.getAuthorImg());
        perSpaceAInfoRes.setAttentionCount(author.getAttentionCount());
        perSpaceAInfoRes.setReaderCount(author.getReaderCount());
        perSpaceAInfoRes.setTotalWords(author.getTotalWords());

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, perSpaceAInfoRes, "");
    }

    /**
     * 作者个人中心作品展示
     * */
    @Override
    public BackResponseEntity<List<HomePageBMoreInfoRes>> queryWorks(DeleteReq deleteReq){
        String id = null;

        if(!ObjectUtils.isEmpty(deleteReq.getId())){
            id = deleteReq.getId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(id)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        LambdaQueryWrapper<BooksInformation> queryWrapper = new QueryWrapper<BooksInformation>().lambda();
        queryWrapper.select(BooksInformation::getBooksId,BooksInformation::getBookName,BooksInformation::getAuthorName,BooksInformation::getBookPic,
                BooksInformation::getBookOnelabel,BooksInformation::getBookTwolabel,BooksInformation::getOneWord,BooksInformation::getContentCount)
                .eq(!ObjectUtils.isEmpty(id),BooksInformation::getAuthorId,id)
                .orderByDesc(BooksInformation::getNewChapterUtime);

        List<BooksInformation> list = booksInformationMapper.selectList(queryWrapper);
        List presenter = list;
        List<HomePageBMoreInfoRes> res1 =presenter;

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, res1, "");

    }
}
